﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace RemoteData.model
{
    public class ChDataItemViewModel : INotifyPropertyChanged
    {
        private int m_channelNo;
        private int m_ngratting;
        private double[] m_waveLen;
        private bool m_updateTrigger;

        private int maxGratting;

        private double[] m_dbValue;

        //public ChDataItemViewModel()
        //{
        //    m_waveLen = new double[TW_App.max_ngrtting];
        //    updateStatus = new UpdateStatus[TW_App.max_ngrtting];
        //    for (int j = 0; j < TW_App.max_ngrtting; j++)
        //    {
        //        updateStatus[j] = new UpdateStatus();
        //    }
        //}

        public ChDataItemViewModel(int ngratting)
        {
            this.maxGratting = ngratting;

            m_waveLen = new double[ngratting];
            m_dbValue = new double[ngratting];
            updateStatus = new UpdateStatus[ngratting * 2];
            for (int j = 0; j < ngratting; j++)
            {
                m_waveLen[j] = double.NaN;
                m_dbValue[j] = double.NaN;
                updateStatus[j * 2] = new UpdateStatus();
                updateStatus[j * 2 + 1] = new UpdateStatus();
            }
        }

        public int channelNo
        {
            get { return m_channelNo; }
            set
            {
                if (value != this.m_channelNo)
                {
                    this.m_channelNo = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public int ngratting
        {
            get { return m_ngratting; }
            set
            {
                if (value != this.m_ngratting)
                {
                    this.m_ngratting = value;
                    NotifyPropertyChanged("ngratting");
                }
            }
        }

        public bool updateTrigger
        {
            get { return m_updateTrigger; }
            set
            {
                m_updateTrigger = value;
                NotifyPropertyChanged();
                NotifyPropertyChanged("updateStatus");
            }
        }
        //public Color[] colors;
        public UpdateStatus[] updateStatus { get; set; }

        public double[] waveLen
        {
            get
            {
                return m_waveLen;
            }
            set
            {
                if (m_waveLen == null)
                {
                    m_waveLen = new double[maxGratting];
                }

                bool dataUpdateFlag = false;

                bool trigger = false;
                for (int i = 0; i < m_waveLen.Length; i++)
                {
                    //if (WaveLenTableWnd.loadStdValue)
                    //{
                    //    double stdValue = WaveLenTableWnd.waveLenStdValue[m_channelNo - 1].waveLen[i];
                    //    double diffValue = value[i] - stdValue;
                    //    double lowThreshold = TW_App.envResource.settingCollection.acqWaveLen.displaySetting.lowWaveLenThreshold;
                    //    double upThreshold = TW_App.envResource.settingCollection.acqWaveLen.displaySetting.upWaveLenThreshold;

                    //    if ((diffValue > 0 && diffValue > upThreshold) || (diffValue < 0 && diffValue < (0 - lowThreshold)))
                    //    {

                    //        //之前的状态不是1，需要刷新
                    //        if (updateStatus[i].status != 1)
                    //        {
                    //            trigger = true;
                    //            updateStatus[i].updateFlag = true;
                    //            updateStatus[i].status = 1;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        //之前的状态不是0，需要刷新
                    //        if (updateStatus[i].status != 0)
                    //        {
                    //            trigger = true;
                    //            updateStatus[i].updateFlag = true;
                    //            updateStatus[i].status = 0;
                    //        }
                    //    }
                    //}

                    double currentValue = double.NaN;
                    if (i < value.Length)
                    {
                        currentValue = value[i];
                    }

                    if (currentValue != this.m_waveLen[i])
                    {
                        dataUpdateFlag = true;
                        this.m_waveLen[i] = currentValue;
                    }
                }
                if (trigger)
                {
                    this.updateTrigger = true;
                }
                if (dataUpdateFlag) NotifyPropertyChanged();
            }
        }

        public double[] dbValue
        {
            get
            {
                return m_dbValue;
            }
            set
            {
                if (m_dbValue == null)
                {
                    m_dbValue = new double[maxGratting];
                }

                bool dataUpdateFlag = false;

                bool trigger = false;
                for (int i = 0; i < m_dbValue.Length; i++)
                {
                    double currentValue = double.NaN;
                    if (i < value.Length)
                    {
                        currentValue = value[i];
                    }

                    if (currentValue != this.m_dbValue[i])
                    {
                        dataUpdateFlag = true;
                        this.m_dbValue[i] = currentValue;
                    }
                }
                if (trigger)
                {
                    this.updateTrigger = true;
                }
                if (dataUpdateFlag) NotifyPropertyChanged();
            }
        }

        private string m_cellBackColorParameter;
        public string cellBackColorParameter
        {
            get { return m_cellBackColorParameter; }
            set
            {
                this.m_cellBackColorParameter = value;
                NotifyPropertyChanged();
            }
        }

        private string m_cellTextColorParameter;
        public string cellTextColorParameter
        {
            get { return m_cellTextColorParameter; }
            set
            {
                this.m_cellTextColorParameter = value;
                NotifyPropertyChanged();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;


        // This method is called by the Set accessor of each property.  
        // The CallerMemberName attribute that is applied to the optional propertyName  
        // parameter causes the property name of the caller to be substituted as an argument.  
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    };
}
